package com.kgc.sbt.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sbt.api.ShowSearchApi;
import com.kgc.sbt.constant.SystemConstant;
import com.kgc.sbt.domain.Goods;
import com.kgc.sbt.domain.GoodsExample;
import com.kgc.sbt.domain.GoodsFroms;
import com.kgc.sbt.mapper.GoodMapper;
import com.kgc.sbt.util.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created on 2021-06-22.
 * <p>
 * Author: a123
 * <p>
 * Description: 展示商品接口业务实现
 */
@Service
public class ShowSearchProvider implements ShowSearchApi {

    @Autowired
    private GoodMapper goodMapper;

    @Override
    public PageSupport<GoodsFroms> showGoods(Integer pageNo, Integer pageSize) {
        // 封装分页数据
        PageSupport<GoodsFroms> pageSupport = new PageSupport<>();

        // 设置页面容量
        if(null == pageSize || pageSize == 0){
            pageSupport.setPageSize(SystemConstant.SYS_INIT_PAGE_SIZE);
        }else {
            pageSupport.setPageSize(pageSize);
        }

        // 设置页码
        if(null == pageNo || pageNo == 0){
            pageSupport.setCurrPageNo(SystemConstant.SYS_INIT_PAGE_NO);
        }else {
            pageSupport.setCurrPageNo(pageNo);
        }


        GoodsExample goodsExample = new GoodsExample();

        pageSupport.setTotalCount((int) goodMapper.countByExample(goodsExample));

        goodsExample.setOrderByClause("launch_time desc");
        goodsExample.setOffset((long) pageSupport.getPageIndex());
        goodsExample.setLimit(pageSupport.getPageSize());

        pageSupport.setData(goodMapper.queryByExample(goodsExample));

        return pageSupport;
    }

    @Override
    public List<GoodsFroms> selectGoodsByName(String name) {

        //List<GoodsFroms> goodsList = new ArrayList<>();

        /*GoodsExample goodsExample = new GoodsExample();

        goodsExample.setOrderByClause("id desc");
        goodsExample.createCriteria().andNameLike("%"+name+"%");*/


        //goodsList = goodMapper.queryByExampleAndName(name);

        return goodMapper.queryByExampleAndName(name);
    }

    @Override
    public List<GoodsFroms> selectHot(String name1) {


        return goodMapper.queryByExampleHot(name1);
    }
}
